// import { defaultCompare } from "./util";
// import { Node } from "./Node";
const defaultCompare = require("./util").default;
const Node = require("./Node").default;


module = class BinarySearchTree {

    constructor(compareFn = defaultCompare) {
        this.compareFn = compareFn; //传入比较的函数
        this.root = null; //默认根节点为空
    }

    /**
     * 插入节点
     * @param {number} key 值
     */
    insert(key) {
        if (this.root == null) {
            this.root = new Node(key);
        }
        this.insertNode(this.root, key);
    }

    insertNode(node, key) {
        if (this.compareFn(node.key, key)) {
            //存到右侧
            if (node.right == null) {
                node.right = new Node(key);
            } else {
                this.insertNode(node.right, key);
            }
        } else {
            //存到左侧
            if (node.left == null) {
                node.left = new Node(key);
            } else {
                this.insertNode(node.left, key);
            }
        }
    }

}